
// src/utils/common.ts

import { MenuItem, BreadcrumbItem } from './types';

// export const getBreadcrumbItems = (path: string, menuItems: MenuItem[]): BreadcrumbItem[] => {
//     const pathParts = path.split('/').filter((x) => x);
//     let currentPath = '';
//     let breadcrumbItems: BreadcrumbItem[] = [];

//     const findItems = (items: MenuItem[], pathParts: string[], index: number) => {
//         for (const item of items) {
//             const itemPathParts = item.path.split('/').filter((x) => x);
//             if (itemPathParts.length !== pathParts.length - index) continue;

//             let match = true;
//             const params: any = {};
//             for (let i = 0; i < itemPathParts.length; i++) {
//                 if (itemPathParts[i].startsWith(':')) {
//                     params[itemPathParts[i].slice(1)] = pathParts[index + i];
//                 } else if (itemPathParts[i] !== pathParts[index + i]) {
//                     match = false;
//                     break;
//                 }
//             }

//             if (match) {
//                 currentPath += `/${pathParts[index]}`;
//                 breadcrumbItems.push({
//                     title: typeof item.title === 'function' ? item.title(params) : item.title,
//                     href: currentPath,
//                 });

//                 if (item.children && index + itemPathParts.length < pathParts.length) {
//                     findItems(item.children, pathParts, index + itemPathParts.length);
//                 }
//                 break;
//             }
//         }
//     };

//     findItems(menuItems, pathParts, 0);
//     return breadcrumbItems;
// };

// 返回完整路径对象
export const findMenuItemPath = (items: any[], targetKey: string): Array<{ label: string; path: string }> => {
  const _find = (arr: any[], key: string, path: any[] = []): any[] | null => {
    for (const item of arr) {
      const currentPath = [...path, { label: item.label, path: item.key }];
      if (item.key === key) return currentPath;
      if (item.children) {
        const found = _find(item.children, key, currentPath);
        if (found) return found;
      }
    }
    return null;
  };
  return _find(items, targetKey) || [];
};
export const anotherFunction = (menuItems: MenuItem[]): void => {
    // 另一个函数的实现
};